package com.hcb.tonguetiphealth.manager;
 

public class Format {
	
	public static String forbidNullStr(String str){
		if (str == null) {
			return "";
		}
		return str;
	}
	
	/**
	 * 将date型字符串转化为long型
	 * @param date 精确至毫秒  格式“yyyy-mm-dd hh:MM:SS:sss”
	 * @return
	 */
	public static long formatDateToLong(String date){
 
		try {
			date = date.replace("-", "").replaceAll(" ", "").replace(":", "");
			if (date.length() <17) {
				while (date.length()<17) {
					date += "0";
				}
			}
			return Long.parseLong(date);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}
	
	/**
	 * 将long型转化为date型字符串
	 * @param long 精确至毫秒  格式“yyyy-mm-dd hh:MM:SS:sss”
	 * @return
	 */
	public static String formatLongToDateStr(long time){
		String temp=" ";
		try {
			String date = String.valueOf(time);
			date = date.replace("-", "").replaceAll(" ", "").replace(":", "");
			if (date.length() <17) {
				while (date.length()<17) {
					date += "0";
				}
			} 		
			temp=date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8)+" "+date.substring(8,10)+":"+date.substring(10,12)+":"+date.substring(12,14);

		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return temp;
	}
	
	/**
	 * 根据给定长度截取字符串
	 * @param str 原始字符串
	 * @param length 指定截取长度
	 * @param end 截取后附加字符串（末尾）
	 * @return
	 */
	public static String getStrFromStrByLength(String str,int length,String end){
		if(str == null || length==0)
			return str;
		if(str.length() <length)
			return str;
		if (str.length() >= length) {
			str = str.substring(0,length)+end;
			return str;
		}
		return "";
	}
}
